package org.batatasframework.chart.columns;

import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.batatasframework.chart.ChartCell;
import org.batatasframework.chart.data.ChartInitializer;

/**
 * 在列表显示的时候，几个字段是单独分开的，在图表统计时，把这几个字段合并成一个字段来看待。
 * 如：地区，现在需要统计到县，那么列表显示为：省、市、县，那么在图表统计里，则应该显示为“省+市+县”的组合
 * @author jackson
 *
 */
public class ColumnAxisMerged extends AbstractChartColumn implements ChartColumnAxis, ChartColumnAxisIterable{

	private final ChartColumnAxis[] columns;
	private String symbol = "";
	private int skipColumn = 0;
	
	private ColumnAxisMerged(String name, int len, ChartColumnAxis... columns) {
		super(name);
		this.columns = new ChartColumnAxis[len];
		for(int i = 0; i < len; i++){
			ChartColumnAxis column = columns[i];
			if(column.rowspan() > rowspan) rowspan = column.rowspan();
			this.columns[i] = column;
		}
	}

	@Override
	public ChartColumn initialize(ChartInitializer initializer) {
		this.ordinal = initializer.nextOrdinal();
		
		for(ChartColumn column : columns){
			column.initialize(initializer);
		}
		
		return this;
	}
	
	public ColumnAxisMerged(String name, ChartColumnAxis... columns) {
		this(name, columns.length, columns);
	}
	
	@Override
	public Iterator<ChartColumnAxis> iterator() {
		return new InternalIterator<>(skipColumn, columns);
	}
	
	public ColumnAxisMerged copy(int len){
		ColumnAxisMerged hcm = new ColumnAxisMerged(name, len, columns);
		hcm.symbol = symbol;
		return hcm;
	}
	
	public void setSymbol(String symbol) {
		this.symbol = symbol;
	}

	@Override
	public int size() {
		int size = 0;
		for(ChartColumn column : columns){
			size += column.size();
		}
		return size;
	}
	
	@Override
	public ChartCell newCell() {
		ChartCell[] mcCells = new ChartCell[columns.length];
		int i = 0;
		for(ChartColumn column : columns){
			mcCells[i++] = column.newCell();
		}
		return new MultipeChartCell(mcCells, symbol);
	}
	
	@Override
	public void resolveHead(String[] heads, int deep) {
		for(ChartColumn column : columns){
			column.resolveHead(heads, deep);
		}
	}
	
	@Override
	public void resolveHead(XSSFRow[] heads, XSSFCellStyle style, int deep) {
		for(ChartColumn column : columns){
			column.resolveHead(heads, style, deep);
		}
	}
}
